#ifndef DBRECORDS_H
#define DBRECORDS_H

#include <QList>
#include <QObject>
#include <QQueue>
#include "settings.h"

class DBRecord;
class LogDB;
class ContactLoadThread;
class CacheItem;

class DBRecords : public QObject
{
    Q_OBJECT
public:
	explicit DBRecords( OptionsClass::FilterOptions filterOptions, int maxRecords, QObject * parent = 0 );
	~DBRecords();
	QList< DBRecord * > dbRecs() const { return m_dbRecs; }
	void setFilterOptions( OptionsClass::FilterOptions f );
	void setEventFilter( OptionsClass::FilterOptions f );
	void setDirectionFilter( OptionsClass::FilterOptions f );
	int maxRecords() const { return m_maxRecords; }
	OptionsClass::FilterOptions filterOptions() const { return m_filterOptions; }
	const DBRecord * at( int i ) { return ( ( i >= 0 && i < m_dbRecs.size() ) ? m_dbRecs.at( i ) : NULL ); }
	const int count() { return m_dbRecs.count(); }

signals:
	void variableChanged();
	void loaded();
	void avatarLoaded( int index );
	void contactLoaded( int index );
	void contactRequested( int id );


public slots:
	void setMaxRecords( int m );
	void load();

private:
	LogDB * m_ldb;
	QList< DBRecord * > m_dbRecs;
	QList< CacheItem * > m_contactCache;
	QQueue< int > m_threadQueue;
	OptionsClass::FilterOptions m_filterOptions;
	int m_maxRecords;
	ContactLoadThread * m_thread;

	void clear();
	void nextInQueue();
	void loadContact( const int id );
	void loadAvatar( const int id );

private slots:
	void processContactRequest( const int id );
	void updateContactCache( const int id );
	void threadFinished();
};

#endif // DBRECORDS_H
